package com.shazhu.nongjt.service.impl;

import java.util.ArrayList;
import java.util.List;


import com.shazhu.common.utils.DateUtils;
import com.shazhu.common.utils.nongjt.ShiroUtils;
import com.shazhu.nongjt.domain.NjtCertificateBase;
import com.shazhu.nongjt.mapper.NjtCertificateBaseMapper;
import com.shazhu.nongjt.service.INjtCertificateBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shazhu.common.utils.StringUtils;


/**
 * 合格证基本信息Service业务层处理
 * 
 * @author shazhu
 * @date 2024-09-28
 */
@Service
public class NjtCertificateBaseServiceImpl implements INjtCertificateBaseService
{
    @Autowired
    private NjtCertificateBaseMapper njtCertificateBaseMapper;

    /**
     * 查询合格证基本信息
     * 
     * @param id 合格证基本信息主键
     * @return 合格证基本信息
     */
    @Override
    public NjtCertificateBase selectNjtCertificateBaseById(Long id)
    {
        return njtCertificateBaseMapper.selectNjtCertificateBaseById(id);
    }

    /**
     * 查询合格证基本信息列表
     * 
     * @param njtCertificateBase 合格证基本信息
     * @return 合格证基本信息集合
     */
    @Override
    public List<NjtCertificateBase> selectNjtCertificateBaseList(NjtCertificateBase njtCertificateBase)
    {
        return njtCertificateBaseMapper.selectNjtCertificateBaseList(njtCertificateBase);
    }

    /**
     * 新增合格证基本信息
     * 
     * @param njtCertificateBase 合格证基本信息
     * @return 结果
     */
    @Override
    public int insertNjtCertificateBase(NjtCertificateBase njtCertificateBase)
    {
        njtCertificateBase.setCreateTime(DateUtils.getNowDate());
        njtCertificateBase.setCreateBy(ShiroUtils.getLoginName());
        return njtCertificateBaseMapper.insertNjtCertificateBase(njtCertificateBase);
    }

    /**
     * 修改合格证基本信息
     * 
     * @param njtCertificateBase 合格证基本信息
     * @return 结果
     */
    @Override
    public int updateNjtCertificateBase(NjtCertificateBase njtCertificateBase)
    {
        njtCertificateBase.setUpdateTime(DateUtils.getNowDate());
        njtCertificateBase.setUpdateBy(ShiroUtils.getLoginName());
        return njtCertificateBaseMapper.updateNjtCertificateBase(njtCertificateBase);
    }

    /**
     * 批量删除合格证基本信息
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteNjtCertificateBaseByIds(Long[] ids)
    {
        return njtCertificateBaseMapper.deleteNjtCertificateBaseByIds(ids);
    }

    /**
     * 删除合格证基本信息信息
     * 
     * @param id 合格证基本信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtCertificateBaseById(Long id)
    {
        return njtCertificateBaseMapper.deleteNjtCertificateBaseById(id);
    }

    /**
     * 批量生成合格证编号
     */
    @Override
    public List<String> batchGenerateNumbers(NjtCertificateBase njtCertificateBase) {
        List<String> numbers = new ArrayList<>();
        List<NjtCertificateBase> certificateList = new ArrayList<>();
        
        int printNumber = njtCertificateBase.getPrintNumber() != null ? njtCertificateBase.getPrintNumber() : 1;
        
        for (int i = 0; i < printNumber; i++) {
            String certificateNo = generateCertificateNo();
            numbers.add(certificateNo);
            
            NjtCertificateBase cert = new NjtCertificateBase();
            cert.setCertificateNo(certificateNo);
            cert.setProductName(njtCertificateBase.getProductName());
            cert.setQuantity(njtCertificateBase.getQuantity());
            cert.setUnit(njtCertificateBase.getUnit());
            cert.setCompanyName(njtCertificateBase.getCompanyName());
            cert.setPrincipalAddress(njtCertificateBase.getPrincipalAddress());
            cert.setProduceAddress(njtCertificateBase.getProduceAddress());
            cert.setTestType(njtCertificateBase.getTestType());
            cert.setTestBy(njtCertificateBase.getTestBy());
            cert.setTestReport(njtCertificateBase.getTestReport());
            cert.setUserSignature(njtCertificateBase.getUserSignature());
            cert.setPrintNumber(1);
            cert.setMobilePhone(njtCertificateBase.getMobilePhone());
            cert.setStatus(1); // 未打印状态
            cert.setCreateTime(DateUtils.getNowDate());
            cert.setCreateBy(ShiroUtils.getLoginName());
            
            certificateList.add(cert);
        }
        
        // 批量插入
        if (!certificateList.isEmpty()) {
            njtCertificateBaseMapper.batchInsert(certificateList);
        }
        
        return numbers;
    }

    /**
     * 批量保存打印记录
     */
    @Override
    public int batchSavePrintRecords(List<String> certificateNos) {
        if (certificateNos == null || certificateNos.isEmpty()) {
            return 0;
        }
        
        // 更新合格证状态为已打印
        return njtCertificateBaseMapper.batchUpdateStatus(certificateNos, 2);
    }

    /**
     * 根据ID或编号查询
     */
    @Override
    public NjtCertificateBase queryByIdOrCode(Long id, String code) {
        if (id != null) {
            return njtCertificateBaseMapper.selectNjtCertificateBaseById(id);
        } else if (StringUtils.isNotEmpty(code)) {
            return njtCertificateBaseMapper.selectByCertificateNo(code);
        }
        return null;
    }

    /**
     * 统计查询
     */
    @Override
    public List<NjtCertificateBase> selectNjtCertificateCountList(NjtCertificateBase njtCertificateBase) {
        return njtCertificateBaseMapper.selectNjtCertificateCountList(njtCertificateBase);
    }

    /**
     * 生成合格证编号
     */
    private String generateCertificateNo() {
        String timestamp = String.valueOf(System.currentTimeMillis());
        String random = String.valueOf((int)((Math.random() * 9 + 1) * 1000));
        return "HZ" + timestamp.substring(timestamp.length() - 8) + random;
    }
}